import useSystemStore from './system'
import useTagsStore from './tags'

export default defineStore('user', {
  state: () => {
    return {
      user: {},
      token: '',
      form: {
        cookie: false,
        username: '',
        password: ''
      }
    }
  },
  actions: {
    setUser (user = {}) {
      this.user = user
    },
    setToken (token = '') {
      this.token = token
    },
    setForm (form) {
      this.form = {
        username: form?.username || '',
        password: form?.password || '',
        cookie: form?.cookie || false
      }
    },
    logOut () {
      // 置空用户信息和token
      this.setUser()
      this.setToken()

      // 置空菜单和按钮
      const systemStore = useSystemStore()
      systemStore.setMenus()
      systemStore.setBtns()

      // 置空Tags菜单
      const tagsStore = useTagsStore()
      tagsStore.$reset()

      // 跳转登录页
      window.location.href = ''
      // console.log(router.currentRoute.value.fullPath)
    }
  },
  persist: true,
})
